package com.oros.db;

import java.io.Serializable;

import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Container.Filter;

public class AEFilterCheckNullId implements Filter, Serializable {


	private static final long serialVersionUID = 125156525749656691L;
    public static boolean CheckFieldIsNotNull = true;
    public static boolean CheckFieldIsNull = false;
	public String columnName;
	public boolean isNotNull; 
	private int hashcode;
    public AEFilterCheckNullId(String columnName,boolean isNotNull)
    {
    	this.columnName = columnName;
    	this.isNotNull = isNotNull;
    	hashcode = 20041970 ^ columnName.hashCode();
    	if (isNotNull) hashcode ^= 1;
    }
	@Override
	public boolean passesFilter(Object itemId, Item item)
			throws UnsupportedOperationException {
		final Property p = item.getItemProperty(columnName);
		        if (null == p) {
		            return false;
		        }
		Object value = p.getValue();
		boolean isReallyNull =(value==null)|| (value instanceof Integer && ((Integer)value).intValue()==0)
				   || (value instanceof Long && ((Long)value).longValue()==0)
				   || (value instanceof String && value.equals("0"));
		return isNotNull?!isReallyNull:isReallyNull;
	}

	@Override
	public boolean appliesToProperty(Object propertyId) {
		return columnName.equals(propertyId);
	}

	@Override
	public int hashCode() {
		return hashcode;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AEFilterCheckNullId other = (AEFilterCheckNullId) obj;
		if (columnName == null) {
			if (other.columnName != null)
				return false;
		} else if (!columnName.equals(other.columnName))
			return false;
		if (isNotNull != other.isNotNull)
			return false;
		return true;
	}

}
